<template>
  <view class="padding-lg bg-gray" style="min-height:100vh">
    <view class="padding bg-white">
      <evan-form :hide-required-asterisk="true" :label-style="labelStyles" 
        ref="form" :model="formInfo" class="no-bottom-border">

        <evan-form-item label="是否为中国大陆身份证：" prop="isMainland" label-position="top">
          <radio-group @change="onIsMainlandChange" style="width:100%">
            <label class="padding-tb-xs margin-right" v-for="it in isMainlandOpts" :key="it.value">
              <radio :value="it.value" :checked="it.value === formInfo.isMainland" />
              <text class="margin-left-sm">{{it.label}}</text>
            </label>
          </radio-group>
        </evan-form-item>

        <evan-form-item label="患者身份证号：" prop="idno" label-position="left">
          <view class="i-input">
            <input type="text" v-model="formInfo.idno" placeholder="请输入" />
          </view>
        </evan-form-item>


        <view class="margin-top-xl">
          <button :disabled="submiting" @click="onSubmit" class="cu-btn lg btn-fluid bg-primary">提交</button>
        </view>
      </evan-form>
    </view>

    <view class="padding bg-white margin-top">
      <view class="text-lg text-primary" style="border-bottom:1px solid #e0e0e0">注意事项</view>
      <view class="padding" style="white-space:pre-wrap">
1. 检测前2小时尽量避免进食，以免引起呕吐。
2. 采样前30分钟不应吸烟、喝酒、咀嚼口香糖。
3. 受测者应正确佩戴口罩，检测前取下口罩，检测后立即戴好口罩，可准备好一个备用口罩，污染后方便立即更换。
4. 如果鼻咽部有病情，比如有血水或者水肿情况，暂时不要进行核酸检测，以免导致出血的症状加重。
5. 核酸检测需要注意手部卫生，可用免洗手消毒液或酒精擦拭双手。
6. 检查前最好不要服用抗病毒的药物或抗生素，以免影响检测的结果。
      </view>
    </view>

    <cu-bar></cu-bar>
  </view>
</template>

<script>
import CheckAuthMixin from '@/mixins/CheckAuthMixin'
import checkIdCard from '@/libs/checkIdCard'
import consts from '@/shared/consts'

const CACHE_KEY = 'ts_matters_submiting'

const FormInfo = {
  uname: '',
  sex: '1',
  age: '',
  idno: '',
  phone: '',
  pat_id: '',
  memo: '',
  addr: '',
  isMainland: '1',
}

export default {
  mixins: [
    CheckAuthMixin,
  ],
  data() {
    return {
      needCheckAuth: false,
      labelStyles: {
        minWidth: '80px',
        textAlign: 'justify',
        paddingRight: '30upx',
        fontSize: '30upx',
        color: 'inherit',
      },
      submiting: false,
      formInfo: Object.assign({}, FormInfo),
      formRules: {
        idno: [
          {
            validator: (rule, value, callback) => {
              if (this.formInfo.isMainland == '1' && !checkIdCard(value)) {
                callback(new Error('请输入正确的身份证号'))
              } else {
                callback()
              }
            },
          }
        ]
      },
      isMainlandOpts: [
        { label: '是', value: '1', },
        { label: '否', value: '2', }
      ]
    }
  },
  watch: {
    submiting(val) {
      if (val) {
        uni.showLoading()
      } else {
        uni.hideLoading()
      }
    },
  },
  onLoad(opts) {
    this.$utils.pLog('== onLoad: ', this.formInfo)
    if (process.env.NODE_ENV != 'production') {
      this.formInfo.idno = consts.testID
    }
  },
  mounted() {
    this.$refs.form.setRules(this.formRules)
  },
  methods: {
    onIsMainlandChange(e) {
      this.formInfo.isMainland = e.detail.value
    },
    onSubmit() {
      this.$refs.form.validate((res) => {
        if (res) {
          this.$utils.pLog('== submiting: ', this.formInfo)

          this.$http({
            url: `/api/InHospital/Query?idnum=${this.formInfo.idno}&isMainland=${this.formInfo.isMainland}`,
            method: 'POST',
            success: (res) => {
              if (res.success) {
                // const c = this.formInfo.idno.charAt(16)
                // const defaultSex = Number(c) % 2 == 0 ? '2' : '1'
                // this.$utils.pLog('----- 默认性别：', defaultSex)
                // const { addr, memo, name, pat_id, sex, tel, idnum } = res.data || {
                //   sex: defaultSex,
                // }
                Object.assign(this.formInfo, {
                  uname: res.data,
                  idno: this.formInfo.idno,
                })

                this.saveState()
                this.gotoStep2()
              } else {
                uni.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }

            }
          })
        }
      })
    },
    saveState() {
      this.$utils.tempPageData.set(this.formInfo, CACHE_KEY)
    },
    loadState() {
      const cached = this.$utils.tempPageData.get(CACHE_KEY)
      if (cached) {
      }
    },
    gotoStep2() {
      uni.navigateTo({
        url: `/pages/hesuanyuyue/step2/index`
      })
    },
  },
}
</script>

<style scoped>
.bg-cus-blue {
  background: #3182F8;
}
.no-bottom-border /deep/ .evan-form-item-container {
  border-bottom: none;
}
</style>